package cn.fl.project.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjCstEmpIntvRecQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstEmpIntvRecService;
import cn.fl.project.vo.PrjCstEmpIntvRecVO;
import cn.fl.system.constant.SysLogConstant;
import net.sf.json.JSONArray;

/**
 * @Description: 客户员工访谈记录表 控制层
 */
@Controller
@RequestMapping("/project/prjcstempintvrec")
public class PrjCstEmpIntvRecController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjCstEmpIntvRecController.class);


	/**
	 * PrjCstEmpIntvRec 服务层BEAN
	 */
	@Autowired
	private IPrjCstEmpIntvRecService prjCstEmpIntvRecService;
	@Autowired
	private ICstBscInfoService cstBscInfoService;
	

	/**
	 * prjBscInfo 服务层BEAN
	 */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");//项目ID
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd = request.getParameter("cstRlCd");
		CstBscInfoVO cstBscInfoVO = null; 
		String cstType = "";
		Long cstId=null;
		if(StringUtils.isNotBlank(prjId)){
			cstBscInfoVO = prjBscInfoService.getCstTypByPrjId(Long.valueOf(prjId));
			if(null!=cstBscInfoVO){
				 cstType = cstBscInfoVO.getCstTypCd();
				 cstId= cstBscInfoVO.getId();
			}
		}
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectCstempintvrecList");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("prjId", prjId);
		view.addObject("cstType",cstType);
		view.addObject("cstId", cstId);
		view.addObject("detailFlag", detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		return view;
	}
	/**
	 * 到添加页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		String cstType = request.getParameter("cstType");//客户类型
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO>  keyList = cstBscInfoService.searchGrpRlTypCdList();
		PrjCstEmpIntvRecVO vo = null;
		if(StringUtils.isNotBlank(id)){
			vo = prjCstEmpIntvRecService.selectById(Long.valueOf(id));
		}
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectCstEmpIntvRecAdd");
		view.addObject("id", id);
		view.addObject("keyList", keyList);	
		view.addObject("prjCstEmpIntvRec", vo);
		view.addObject("prjId", prjId);
		view.addObject("cstId", cstId);
		view.addObject("cstType", cstType);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到详细页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstType = request.getParameter("cstType");
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectCstEmpIntvRecDetail");
		PrjCstEmpIntvRecVO vo = null;
		if(StringUtils.isNotBlank(id)){
			vo = prjCstEmpIntvRecService.selectById(Long.valueOf(id));
		}
		view.addObject("id", id);
		view.addObject("prjCstEmpIntvRec", vo);
		view.addObject("cstType", cstType);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系类型（客户、担保人）
		Pager page = new Pager();
		PrjCstEmpIntvRecQueryCondition qc = new PrjCstEmpIntvRecQueryCondition();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if(StringUtils.isNotBlank(prjId)){
			qc.setCstId(Long.valueOf(cstId));
		}
		if(StringUtils.isNotBlank(cstId)){
			qc.setPrjId(Long.valueOf(prjId));
		}
		if(StringUtils.isNotBlank(cstRlCd)){
			qc.setCstRlCd(cstRlCd);
		}
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		page.setCondition(qc);
		Pager pager =  prjCstEmpIntvRecService.searchListPage(page);
		List<PrjCstEmpIntvRecVO> list = (List<PrjCstEmpIntvRecVO>) pager.getResultList();
		ListPageVO<PrjCstEmpIntvRecVO>  listPage = new ListPageVO<PrjCstEmpIntvRecVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(pager), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		List<Long> idList = new ArrayList<Long>();
		JSONArray idsArray = JSONArray.fromObject(ids);
		for (Object object : idsArray) {
			idList.add(Long.valueOf(object.toString()));
		}
		try {
			this.prjCstEmpIntvRecService.removeByIds(idList);
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+ids+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+ids+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+ids+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			PrjCstEmpIntvRecVO vo = this.prjCstEmpIntvRecService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjCstEmpIntvRecVO entity = null;
		try {
			entity = new PrjCstEmpIntvRecVO();
			Long recId = null;
			PrjCstEmpIntvRecVO vo = getPrjCstEmpIntvRecParam(request);
			if(StringUtils.isNotBlank(id)){
				//修改沟通记录
				vo.setId(Long.valueOf(id));
				this.prjCstEmpIntvRecService.modify(vo);
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
			}else{
				//获取沟通记录
				vo.setCrtTm(new Date());
				vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				vo.setIsDel(BaseConstant.IS_YESNO_NO);
				recId = this.prjCstEmpIntvRecService.add(vo);
			}
		
			result.setData(recId);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	private PrjCstEmpIntvRecVO getPrjCstEmpIntvRecParam(HttpServletRequest request) {
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String nm = request.getParameter("nm");
		String posCd = request.getParameter("posCd");
		String tel = request.getParameter("tel");
		String commRec = request.getParameter("commRec");
		String assRlCd = request.getParameter("assRlCd");
		String cstRlCd = request.getParameter("cstRlCd");
		PrjCstEmpIntvRecVO vo = new PrjCstEmpIntvRecVO();
		if(StringUtils.isNotBlank(prjId)){
			vo.setPrjId(Long.valueOf(prjId));
		}
		if(StringUtils.isNotBlank(cstId)){
			vo.setCstId(Long.valueOf(cstId));
		}
		if(StringUtils.isBlank(nm)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"名称不能为空！");
		}
			vo.setNm(nm);
			vo.setPosCd(posCd);
			vo.setCstRlCd(cstRlCd);
		if(StringUtils.isBlank(tel)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"联系方式不能为空！");
		}
		if(StringUtils.isNotBlank(tel)){
			if(!StringUtils.isMobilePhoneAndFixedPhone(tel)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604,"联系方式格式错误");
			}
		}
		vo.setTel(tel);
		if(StringUtils.isBlank(commRec)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"访谈记录不能为空！");
		}
			vo.setCommRec(commRec);
		
			vo.setAssRlCd(assRlCd);
		return vo;
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		PrjCstEmpIntvRecVO entity = null;
		try {
			entity = new PrjCstEmpIntvRecVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.prjCstEmpIntvRecService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toGuarListPage.do", method = { RequestMethod.GET})
	public ModelAndView toGuarListPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");//项目ID
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd = request.getParameter("cstRlCd");
		String cstId = request.getParameter("cstId");
		String cstType = request.getParameter("cstTypCd");
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectGuarEmpIntvRecList");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("prjId", prjId);
		view.addObject("cstType",cstType);
		view.addObject("cstId", cstId);
		view.addObject("detailFlag", detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		return view;
	}
	
	/**
	 * 到添加页面
	 */
	@RequestMapping(value = "/toGuarAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarAddPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		String cstType = request.getParameter("cstType");//客户类型
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO>  keyList = cstBscInfoService.searchGrpRlTypCdList();
		PrjCstEmpIntvRecVO vo = null;
		if(StringUtils.isNotBlank(id)){
			vo = prjCstEmpIntvRecService.selectById(Long.valueOf(id));
		}
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectGuarEmpIntvRecAdd");
		view.addObject("id", id);
		view.addObject("keyList", keyList);	
		view.addObject("prjCstEmpIntvRec", vo);
		view.addObject("prjId", prjId);
		view.addObject("cstId", cstId);
		view.addObject("cstType", cstType);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到详细页面
	 */
	@RequestMapping(value = "/toGuarDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstType = request.getParameter("cstType");
		ModelAndView view = new ModelAndView("project/prjcstempintvrec/projectGuarEmpIntvRecDetail");
		PrjCstEmpIntvRecVO vo = null;
		if(StringUtils.isNotBlank(id)){
			vo = prjCstEmpIntvRecService.selectById(Long.valueOf(id));
		}
		view.addObject("id", id);
		view.addObject("prjCstEmpIntvRec", vo);
		view.addObject("cstType", cstType);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

}

